/**
 * @Author: 魏巍
 * @Date:   2018-09-15T14:52:41+08:00
 * @Email:  524314430@qq.com
 * @Filename: app.js
 * @Last modified by:   魏巍
 * @Last modified time: 2018-09-22T00:46:51+08:00
 * @Copyright: free
 */

//APP create@Pangys
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const cors = require('koa2-cors');
const controller = require('./controller'); //扫描控制器
const templating = require('./templating'); //模板编译
const rest = require('./rest'); //restAPI
const session = require('./session'); //session配置

const fileServ = require('./file-server'); //文件资源服务

//const ws = require('./ws/jnbank');

const app = new Koa();

const isProduction = process.env.NODE_ENV === 'production'; //判断是否为生产环境

app.use(cors()); //跨域-cors
app.use(bodyParser()); //解析请求
app.use(session); //添加session

app.use(async (ctx, next) => { //输出请求
  console.log(`${ctx.request.method} ${ctx.request.url}...`); //输出收到的请求
  await next();
});

if (!isProduction) { //静态文件加载
  let staticFiles = require('./static-file');
  app.use(staticFiles('/static/', __dirname + '/static'));
}

app.use(templating('view', { //模板加载
  noCache: !isProduction, //非生产->不缓存
  watch: !isProduction
}));

app.use(rest.restify()); //REST接口
app.use(controller()); //扫描加载控制器

//ws.ws();

app.listen(require('./config').port); // app应用服务在端口3000监听:
fileServ.listen(8099) //文件服务在8099


console.log('app started...')
console.log(`http serve url http://localhost:${require('./config').port}`)
